﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OUnsuitableProdEditV2 : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改, Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get { return "OUnsuitableProdList"; }
        }

        #region
        /// <summary>
        /// 需要修改的用户ID
        /// </summary>
        protected int UnsuitabledID
        {
            get
            {
                return GetRequestValue<int>("UnsuitabledID", 0);
            }
        }
        /// <summary>
        /// 添加类型
        /// </summary>
        protected int AddType
        {
            get
            {
                return GetRequestValue<int>("AddType", 0);
            }
        }

        private OUnsuitableProdInfo _obj;
        protected OUnsuitableProdInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = OUnsuitableProdController.GetById(UnsuitabledID);

                if (AddType == 1)
                    _obj.IssueDate = DateTime.Now;

                return _obj;
            }
        }

        private List<OUnsuitableProdImgInfo> _listimg = null;
        protected List<OUnsuitableProdImgInfo> ListImg
        {
            get
            {
                if (_listimg == null)
                {
                    _listimg = OUnsuitableProdController.OUnsuitableProdImg_GetAllByUnsuitabledID(UnsuitabledID);
                }
                return _listimg;
            }
        }

        private List<OUnsuitableProdDetailInfo> _listdetail = null;
        protected List<OUnsuitableProdDetailInfo> ListDetail
        {
            get
            {
                if (_listdetail == null)
                {
                    _listdetail = OUnsuitableProdController.OUnsuitableProdDetail_GetAllByUnsuitabledID(UnsuitabledID);
                }
                return _listdetail;
            }
        }

        protected void BindList()
        {

            List<OUnsuitableProdImgInfo> listimg = ListImg;
            int listimgnum = listimg.Count;
            if (listimgnum < 6)
            {
                for (int numtemp = listimgnum; numtemp < 6; numtemp++)
                {
                    listimg.Add(new OUnsuitableProdImgInfo() { UnImgID = numtemp });
                }
            }

            for (int numtemp = 1; numtemp <= listimg.Count; numtemp++)
            {
                listimg[numtemp - 1].ShowIndex = numtemp;
            }

            this.rpImg.DataSource = listimg;
            this.rpImg.DataBind();


            List<OUnsuitableProdDetailInfo> detail1 = ListDetail.Where(e => e.DetailType == "返回").ToList();
            int dcnum = detail1.Count;
            if (dcnum < 10)
            {
                for (int numtemp = dcnum; numtemp < 10; numtemp++)
                {
                    detail1.Add(new OUnsuitableProdDetailInfo() { DetailType = "返回", IsReturned = false, IsDefectivePrd = false });
                }
            }
            for (int numtemp = 0; numtemp < detail1.Count; numtemp++)
            {
                detail1[numtemp].ShowIndex = numtemp;

            }
            this.rpDetail1.DataSource = detail1;
            this.rpDetail1.DataBind();


            List<OUnsuitableProdDetailInfo> detail2 = ListDetail.Where(e => e.DetailType == "报废").ToList();
            dcnum = detail2.Count;
            if (dcnum < 10)
            {
                for (int numtemp = dcnum; numtemp < 10; numtemp++)
                {
                    detail2.Add(new OUnsuitableProdDetailInfo() { DetailType = "报废", IsReturned = false, IsDefectivePrd = false, IsScrap = false });
                }
            }
            for (int numtemp = 0; numtemp < detail2.Count; numtemp++)
            {
                detail2[numtemp].ShowIndex = numtemp;
            }

            this.rpDetail2.DataSource = detail2;
            this.rpDetail2.DataBind();
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();

            }
            else
            {
                BindList();
            }
        }

        protected void addmenu()
        {
            OUnsuitableProdInfo obj2 = Request.SerializableObject<OUnsuitableProdInfo>();

            //2015-01-05
            if (!obj2.FindPrdWorkShop && !obj2.FindWarehouse)
            {
                AlertJson("发现场所 必需选一个.");
                return;
            }
            if (!obj2.ERPFinished && !obj2.ERPNotFinished)
            {
                AlertJson("ERP 必需选一个.");
            }
            if (!obj2.NotionReturn && !obj2.NotionOverhaul && !obj2.NotionSpecial && !obj2.NotionScrap)
            {
                AlertJson("处理意见 必需选一个.");
            }


            obj2.AddType = AddType;
            obj2.SupplerName = GetRequestValue<string>("dwz.opeV2Lookup.SupplerName", "");

            List<OUnsuitableProdImgInfo> listImg = new List<OUnsuitableProdImgInfo>();
            List<OUnsuitableProdDetailInfo> listDetail = new List<OUnsuitableProdDetailInfo>();

            char[] caSplit = new char[] { ',' };
            string[] titles = GetRequestValue<string>("title", "").Split(caSplit);
            string[] IMGAbsolutePaths = GetRequestValue<string>("IMGAbsolutePath", "").Split(caSplit);
            string[] IMGRelativelys = GetRequestValue<string>("IMGRelatively", "").Split(caSplit);

            //
            string[] DetailTypes = GetRequestValue<string>("DetailType", "").Split(caSplit);
            string[] DItemCodes = GetRequestValue<string>("DItemCode", "").Split(caSplit);
            string[] DItemNames = GetRequestValue<string>("DItemName", "").Split(caSplit);
            string[] DQuantitys = GetRequestValue<string>("DQuantity", "").Split(caSplit);
            string[] IsReturneds = GetRequestValue<string>("IsReturned", "").Split(caSplit);
            string[] IsDefectivePrds = GetRequestValue<string>("IsDefectivePrd", "").Split(caSplit);
            string[] IsScraps = GetRequestValue<string>("IsScrap", "").Split(caSplit);

            for (int index = 0; index < IMGRelativelys.Count(); index++)
            {
                if (IMGRelativelys[index] != "")
                {
                    OUnsuitableProdImgInfo itm = new OUnsuitableProdImgInfo();
                    itm.Title = titles[index];
                    itm.IMGAbsolutePath = IMGAbsolutePaths[index];
                    itm.IMGRelatively = IMGRelativelys[index];

                    listImg.Add(itm);
                }
            }

            for (int index = 0; index < DItemCodes.Count(); index++)
            {
                if (DItemCodes[index] != "")
                {
                    OUnsuitableProdDetailInfo itm = new OUnsuitableProdDetailInfo();
                    itm.DetailType = DetailTypes[index];
                    itm.DItemCode = DItemCodes[index];
                    itm.DItemName = DItemNames[index];
                    itm.DQuantity = Convert.ToDecimal(DQuantitys[index]);
                    itm.IsReturned = Convert.ToBoolean(IsReturneds[index] == "checked" || IsReturneds[index].ToLower() == "true" ? true : false);
                    itm.IsDefectivePrd = Convert.ToBoolean(IsDefectivePrds[index] == "checked" || IsDefectivePrds[index].ToLower() == "true" ? true : false);
                    itm.IsScrap = Convert.ToBoolean(IsScraps[index] == "checked" || IsScraps[index].ToLower() == "true" ? true : false);

                    //处理 不良品/报废原材料出库单 必选
                    if (itm.DetailType == "报废" && itm.IsDefectivePrd == false && itm.IsScrap == false)
                    {
                        AlertJson("不良品转库 或 报废处理 必需选一个.");
                    }

                    listDetail.Add(itm);
                }
            }

            if (AddType == 0)
            {
                this.AddLog(Permission.修改, obj, obj2);
                if (OUnsuitableProdController.Update(obj2, listImg, listDetail))
                    SuccessAlertJson("修改成功");
                else
                    ErrorJson("修改失败", this.DWZRef);
            }
            else if (AddType == 1)
            {
                this.AddLog(Permission.新增, null, obj2);

                int r = OUnsuitableProdController.Add(obj2, listImg, listDetail);
                if (r > 0)
                    SuccessAlertJson("补充及追加成功");
                else
                    ErrorJson("补充及追加失败", this.DWZRef);
            }
        }
    }
}